class DeliveriesController < ApplicationController
  before_action :set_delivery, only: [:show, :pause, :resume, :stop]

  # GET /deliveries
  # GET /deliveries.json
  def index
    @deliveries = Delivery.all
  end

  # GET /deliveries/1
  # GET /deliveries/1.json
  def show
  end

  # PATCH/PUT /deliveries/1
  # PATCH/PUT /deliveries/1.json
  def update
    respond_to do |format|
      if @delivery.update(delivery_params)
        format.html { redirect_to @delivery, notice: 'Delivery was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @delivery.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /deliveries/1/pause
  def pause
    @delivery.pause
    render text: promotion_mails_path
  end

  # PATCH/PUT /deliveries/1/resume
  def resume
    @delivery.resume
    render text: promotion_mails_path
  end

  def stop
    @delivery.can_stop?
    render text: promotion_mails_path
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_delivery
      @delivery = Delivery.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def delivery_params
      params[:delivery]
    end
end
